↜ Back to index Introduction to Numerical Analysis 1
Part a—Lecture 1
We start with a review of what you learned in your first year classes 情報・計算科学基礎 and 計算科学.
Fortran knowledge (Fortran知識)
You need a working Fortran compiler: gfortran
From the class Computational science (計算科学) you should already know most of the Fortran language that we will be using:
basic arithmetic:
1 + 2
,x = sqrt(2.0)
,x**2
, …comments (コメント): Anything on a line after
!
is ignored.implicit none ! this is a comment real x ! comment on a line with code end
program end:
end
variable declaration (変数宣言)
integer i ! integer variable real x ! single precision real variable 単精度実数変数
printing to standard output (標準出力)
write(*,*) 'hello' print *, 'hello' ! I prefer this
reading from standard input (キーボード入力)
read *, x, i ! user can input values for x, i ! separated by space, comma (,) or Enter
if
condition (条件分岐)if (1 < 2) then ! can also be (1 .lt. 2) print *, 'true' else print *, 'false' end if
do
loops (ループ)do i=1,5 print *, 'square of ', i, ' is ', i**2 end do
built-in functions:
abs
,cos
,sin
,mod
, …
I prefer using the free-form Fortran style that does not require 6 spaces at the beginning of each line. The file extension (拡張子) must be .f90
.
For details see Fortran cheatsheet.
Why do we need to compile Fortran code?
Let us consider the program that computes a factorial (階乗) of an input number.
! Program that computes a factorial
implicit none
integer i, n, f
print *, "Enter n"
read *, n
= 1
f
do i = 1,n
= f * i
f end do
print *, "Factorial of ", n, " = ", f
end
We can only run a Fortran program after we compile it. In the terminal (Cygwin for example):
$ gfortran fact.f90
$ ./a.exe
Enter n
5
Factorial of 5 = 120
(Advanced) What is in the file a.exe
?
Let us try to print it out:
$ cat a.exe
You will see a lot of strange characters in the output. The file a.exe
is a binary file that does not contain text but contains a sequence of numbers that represent the instructions for your computer, specifically the CPU (central processing unit). This is a chip that reads a sequence of simple instructions from the computer memory and performs them one at a time. Some of these instructions read data from memory, some do arithmetic operations on the data and some print stuff to the screen.
We can print out the instructions in a.exe
in a readable form using objdump
program:
$ objdump -d a.exe
a.exe: file format elf64-x86-64
Disassembly of section .init:
0000000000001000 <_init>:
1000: f3 0f 1e fa endbr64
1004: 48 83 ec 08 sub $0x8,%rsp
1008: 48 8b 05 c9 2f 00 00 mov 0x2fc9(%rip),%rax # 3fd8 <__gmon_start__>
100f: 48 85 c0 test %rax,%rax
1012: 74 02 je 1016 <_init+0x16>
1014: ff d0 callq *%rax
1016: 48 83 c4 08 add $0x8,%rsp
101a: c3 retq
...
You can see the instructions for subtraction (sub
), addition (add
), jumps to other instructions (je
: jump if equal) etc. This is what your computer sees when you run the compiled Fortran program.
Exercises (練習問題)
Here are a few exercises for today to review your Fortran knowledge.
Exercise 1. Running a Fortran program
Try to run the following program. Make sure to use file extension (拡張子) .f90
. If you use .f
, you will have to enter 6 spaces at the beginning of each line.
print *, "Hello"
end
For example, save it in a file hello.f90
, and then compile and run it as:
$ gfortran hello.f90
$ ./a.exe
Hello
Text following $
are commands to be typed on the terminal command line (端末) (cygwin
on Windows). Lines without $
show the output of the commands. Replace ./a.exe
with ./a.out
if you are on macOS or Linux.
It might be useful to change the name of the compiled program. Change the name of the executable file in the previous exercise to hello.exe
.
$ gfortran hello.f90 -o hello.exe
$ ./hello.exe
Hello
Exercise 2. These are some tricky points when working with numbers in Fortran (and on a computer in general). Make sure that you understand the outputs of these programs.
What does the following program print? Why?
= 2.5 i if (i == 2.5) then print *, "Equal" else print *, "Math is wrong!" endif end
What does this program print? Why?
implicit none real x, y, z = 1 x = x / 2 y = 1 / 2 z print *, y print *, z end
What does this program print? Why?
implicit none real x, s = 2 x = x**2 s = 3 x print *, s end
Exercise 3. What is wrong the following Fortran program?
print *, 'The value of cos(2pi) is', cos(2 * pi)
end
Exercise 4. Write a program that reads an integer and prints whether it is even (遇) or odd (奇).
Submit the code to Acanthus.
Exercise 5. Solving the quadratic equation
Write a program that finds the solutions x of the quadratic equation ax^2 + bx + c = 0 given a, b and c from the user. Print the solutions, if any, from smaller to larger.
Example. Suppose that the user enters 1 -3 2
, the program should output something like:
x1 = 1.00000
x2 = 2.00000
Submit the code to Acanthus.
Test questions
How do you compile Fortran program
equation.f90
?How do you run compiled Fortran program?
What is the difference between Fortran programs in a file with extension
.f
and extension.f90
?What does
implicit none
do?